#include "encode.h"
#include "sys.h"
                                            

void Encode1_init(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
  TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
	
	
  TIM_ICInitTypeDef TIM_ICInitStructure;    

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);    
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);  
		
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;          
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;                             
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;	
  GPIO_Init(GPIOA, &GPIO_InitStructure);                         

  GPIO_PinAFConfig(GPIOA,GPIO_PinSource6,GPIO_AF_TIM3);        
  GPIO_PinAFConfig(GPIOA,GPIO_PinSource7,GPIO_AF_TIM3);         
	
  TIM_DeInit(TIM3);
  TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
  TIM_TimeBaseStructure.TIM_Period = 65535; 	                    
  TIM_TimeBaseStructure.TIM_Prescaler=0;                      
  TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;      
  TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;          
  TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);
	
  TIM_ICStructInit(&TIM_ICInitStructure);
	
  TIM_EncoderInterfaceConfig(TIM3,TIM_EncoderMode_TI12,TIM_ICPolarity_Rising,TIM_ICPolarity_Rising );
	

  TIM_SetCounter(TIM3,0);              
  TIM_Cmd(TIM3,ENABLE);                                          
}

int read_encoder1(void)
{
	int encoder_num1;
	
	encoder_num1 = (int)((int16_t)(TIM3->CNT));
	
	TIM_SetCounter(TIM3, 0);
	
	return encoder_num1;
}



void Encode2_init(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
  TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
  TIM_ICInitTypeDef TIM_ICInitStructure;    

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);    
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);  
		
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;          
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;                             
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;	
  GPIO_Init(GPIOB, &GPIO_InitStructure);                         

  GPIO_PinAFConfig(GPIOB,GPIO_PinSource6,GPIO_AF_TIM4);        
  GPIO_PinAFConfig(GPIOB,GPIO_PinSource7,GPIO_AF_TIM4);         
	
  TIM_DeInit(TIM4);
  TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
  TIM_TimeBaseStructure.TIM_Period = 65535; 	                    
  TIM_TimeBaseStructure.TIM_Prescaler=0;                      
  TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;      
  TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;          
  TIM_TimeBaseInit(TIM4,&TIM_TimeBaseStructure);
	
  TIM_ICStructInit(&TIM_ICInitStructure);
	
  TIM_EncoderInterfaceConfig(TIM4,TIM_EncoderMode_TI12,TIM_ICPolarity_Rising,TIM_ICPolarity_Rising );
	

  TIM_SetCounter(TIM4,0);              
  TIM_Cmd(TIM4,ENABLE);                                          
}

int read_encoder2(void)
{
	int encoder_num2;
	
	encoder_num2 = (int)((int16_t)(TIM4->CNT));
	
	TIM_SetCounter(TIM4, 0);
	
	return encoder_num2;
}

